package com.samphanie.auiu.library.log;

import androidx.annotation.NonNull;

import com.samphanie.auiu.library.utils.AuiuStackTraceUtil;

import java.util.Arrays;
import java.util.List;

/**
 * @author ZSY
 * Tips :
 * 1. 打印堆栈信息
 * 2. File输出
 * 3. 模拟控制台
 */
public class AuiuLog {

    private static final String OASE_LOG_PACKAGE;

    static {
        String className = AuiuLog.class.getName();
        OASE_LOG_PACKAGE = className.substring(0, className.lastIndexOf('.') + 1);
    }

    public static void v(Object... contents) {
        log(AuiuLogType.V, contents);
    }

    public static void vt(String tag, Object... contents) {
        log(AuiuLogType.V, tag, contents);
    }

    public static void e(Object... contents) {
        log(AuiuLogType.E, contents);
    }

    public static void et(String tag, Object... contents) {
        log(AuiuLogType.E, tag, contents);
    }

    public static void a(Object... contents) {
        log(AuiuLogType.A, contents);
    }

    public static void at(String tag, Object... contents) {
        log(AuiuLogType.A, tag, contents);
    }

    public static void d(Object... contents) {
        log(AuiuLogType.D, contents);
    }

    public static void dt(String tag, Object... contents) {
        log(AuiuLogType.D, tag, contents);
    }

    public static void i(Object... contents) {
        log(AuiuLogType.I, contents);
    }

    public static void it(String tag, Object... contents) {
        log(AuiuLogType.I, tag, contents);
    }

    public static void w(Object... contents) {
        log(AuiuLogType.W, contents);
    }

    public static void wt(String tag, Object... contents) {
        log(AuiuLogType.W, tag, contents);
    }

    public static void log(@AuiuLogType.TYPE int type, Object... contents) {
        log(type, AuiuLogManager.getInstance().getConfig().getGlobalTag(), contents);
    }

    public static void log(@AuiuLogType.TYPE int type, @NonNull String tag, Object... contents) {
        log(AuiuLogManager.getInstance().getConfig(), type, tag, contents);
    }

    public static void log(@NonNull AuiuLogConfig config, @AuiuLogType.TYPE int type, @NonNull String tag, Object... contents) {

        if (!config.enable()) {
            return;
        }

        StringBuilder sb = new StringBuilder();
        if (config.includeThread()) {
            String threadInfo = AuiuLogConfig.HI_THREAD_FORMATTER.format(Thread.currentThread());
            sb.append(threadInfo).append("\n");
        }
        if (config.stackTraceDepth() > 0) {
            String stackTrace = AuiuLogConfig.HI_STACK_TRACE_FORMATTER.format(AuiuStackTraceUtil.getCroppedRealStackTrack(new Throwable().getStackTrace(), OASE_LOG_PACKAGE, config.stackTraceDepth()));
            sb.append(stackTrace).append("\n");
        }
        String body = parseBody(contents, config);
        sb.append(body);
        // Log.println(type, tag, body);
        List<AuiuLogPrinter> printers = config.printers() != null ? Arrays.asList(config.printers()) : AuiuLogManager.getInstance().getPrinters();
        if (printers == null) {
            return;
        }

        // 打印Log
        for (AuiuLogPrinter printer : printers) {
            printer.print(config, type, tag, sb.toString());
        }
    }

    private static String parseBody(@NonNull Object[] contents, @NonNull AuiuLogConfig config) {

        if (config.injectJsonParser() != null) {
            return config.injectJsonParser().toJson(contents);
        }

        StringBuilder sb = new StringBuilder();
        for (Object content : contents) {
            sb.append(content.toString()).append(";");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
